using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Rensoft.CRM.Client.Editors;
using Rensoft.CRM.Client.AccountsAPI;

namespace Rensoft.CRM.Client.ItemViews
{
    public partial class BaseView : UserControl
    {
        public event EventHandler ItemCountChanged;

        private List<ListViewItem> selectedItems;
        private Program program;
        private int itemCount;

        [Browsable(true)]
        public string Title
        {
            get { return TitleLabel.Text; }
            set { TitleLabel.Text = value; }
        }

        public List<ListViewItem> SelectedItems
        {
            get { return selectedItems; }
        }

        public bool CanUpdate
        {
            get { return !LoadBackgroundWorker.IsBusy; }
        }

        public int ItemCount
        {
            get { return itemCount; }
        }

        public Program Program
        {
            get { return program; }
        }

        public virtual Image NewButtonImage
        {
            get { throw new NotImplementedException(); }
        }

        public BaseView()
        {
            InitializeComponent();

            selectedItems = new List<ListViewItem>();

            itemCount = 0;
            ItemCountChanged += new EventHandler(OnItemCountChanged);
        }

        public BaseView(Program program) : this()
        {
            this.program = program;
        }

        public void UpdateItems()
        {
            program.Cursor = Cursors.WaitCursor;
            LoadBackgroundWorker.RunWorkerAsync();
        }

        protected virtual void PerformLoad() { }

        protected virtual void OnRetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e) { }
        protected virtual void OnItemCountChanged(object sender, EventArgs e) { }

        protected void OnRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            program.Cursor = Cursors.Default;

            if (e.Error != null)
            {
                throw e.Error;
            }

            LoadComplete(e);
        }

        protected virtual void LoadComplete(RunWorkerCompletedEventArgs e) { }

        protected void UpdateItemCount(int itemCount)
        {
            this.itemCount = itemCount;
            ItemCountChanged(this, EventArgs.Empty);
        }

        private void LoadBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            AccountsService service = AccountsService.CreateInstance();
            MethodInvoker testMethod = new MethodInvoker(service.Test);

            if (program.ConfirmConnection(service, testMethod))
            {
                this.PerformLoad();
            }
        }

        protected virtual void PerformEditorOpen(object tag) { }

        private void ItemsListView_DoubleClick(object sender, EventArgs e)
        {
            if (ItemsListView.SelectedIndices.Count == 1)
            {
                PerformEditorOpen(ItemsListView.FocusedItem.Tag);
            }
        }

        internal virtual BaseEditor GetEditor()
        {
            throw new NotImplementedException();
        }
    }
}
